<?php
    function generaHeader() {
        echo '
          <div id="header">
            <h1><a href="index.php">News forum</a></h1>
          </div>
        '; 

    }
    function generaMenusx() {
        echo '
            <div id="menusx" class="menu">
                <div class="nav">';
                echo '<ul>';
		if(isset($_GET['cat']) && $_SESSION['logged']) {
	                $categoria = cleanSimpleString($_GET['cat'],30);
	                $qry = "SELECT NOME FROM Categorie WHERE Nome = '$categoria'";
	                $check = mysql_query($qry);
	                if (mysql_num_rows($check)==1) echo '<li><a href="nuovoThread.php?cat='.$categoria.'">Nuovo Thread</a></li>';
	                //è un controllo quasi inutile, però...
        	}
		else if(isset($_GET['t'])) {
	                $thread = cleanNumber($_GET['t']);
	                $qry = "SELECT Id, Stato FROM Threads WHERE Id = $thread";
	                $check = mysql_query($qry);
			$arr = mysql_fetch_assoc($check);
	                if (mysql_num_rows($check)==1 && $arr['Stato']=='Aperto') echo '<li><a href="nuovoPost.php?t='.$thread.'">Rispondi</a></li>';
		}      
		else if($_SERVER['PHP_SELF'] == '/index.php' && $_SESSION['Status']=='admin') {
			echo '<li><a href="nuovaCategoria.php'.$thread.'">Nuova Categoria</a></li>';
		}
		$qry= "SELECT Nome FROM Categorie ORDER BY Ordine";
		$check = mysql_query($qry);
		$array = mysql_fetch_assoc($check);
		echo '<li id="cliccare" class="navCat"><a>Categorie</a></li><li><ul id="sottoMenu" class="show">';
		while($array) {
			echo '<li><a href="categoria.php?cat='.$array['Nome'].'">'.$array['Nome'].'</a></li>';
			$array = mysql_fetch_assoc($check);
		}
                echo'</ul></li>
                
                <li><a href="cerca.php">Ricerca</a></li></ul>
                
                
                </div>
            </div>
        ';

	echo '
		<script type="text/javascript">
		var prev="null";

		window.onload=function() {
		if(document.getElementsByTagName && document.getElementById){ 
		    document.getElementById("sottoMenu").className="hide";
		    BuildList();
		    if(document.getElementById("sottoPosta")) {
			    document.getElementById("sottoPosta").className="hide";
			    BuildPosta();
			}
		    }
		}

		function BuildList() {
			var hs=document.getElementById("cliccare");
		    	hs.onclick=function(){
				document.getElementById("sottoMenu").className=(document.getElementById("sottoMenu").className=="show") ? "hide" : "show";       	
			}			
		}
		</script>
	';

    }

    function generaMenuLogin() {
        echo '
            <div id="menudx" class="menu">
            <div class="NomeUtente">                
                ';
        if (!isset($_SESSION['logged']) || !$_SESSION['logged']){
		$_SERVER['QUERY_STRING'] = str_replace(array('&amp;', '&'), array('&', '&amp;'),$_SERVER['QUERY_STRING']);
			echo '
				<form name="login" method="post" action="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'">
				<p>';
                echo 'Utente: <input id="Utente" name="Utente" type="text" alt="Utente" value="'.$_SESSION['Utente'].'">
                </p>
                <p>
				Password: <input id="Password" type="password" name="Password" alt="Password">
				</p>
				<p>
                			<input type="submit" name="Submit" value="Login"></p>
				</form><p><a href="smarrita.php">Password Dimenticata?</a></p>';
				if (isset($_SESSION['logged']) && !$_SESSION['logged'])  {
					echo'<p id="error">Login non riuscito</p>';
					unset($_SESSION['logged']);
				}
				echo'
				</div>
				<div class="nav">
				<ul>
					<li><a href="registrati.php">Registrati</a></li>

				';
			}
		else echo '<p class="inline">'.$_SESSION['Utente'].'</p>
			  <p class="inline">
					<form name="logout" method="post" action="index.php">
					<input type="submit" name="Logout" value="Logout">
					</form>
					</div><div class="nav">
					<ul>						
						<li><a href="personal.php">Profilo</a></li>
						<li id="posta" class="navCat"><a>Posta</a></li>
						<li><ul id="sottoPosta" class="show">
							<li><a href="messaggio.php">Invia un Messaggio</a></li>
							<li><a href="posta.php">I miei Messaggi</a></li>
						</ul></li>
					';

			if(isset($_SESSION['Status']) && $_SESSION['Status']=='admin') {
				echo '<li><a href="pannello.php">Pannello</a></li>
						';
				
			}
		echo'
			</ul>
			
		<script type="text/javascript">
		var prev="null";

		function BuildPosta() {
			var hs=document.getElementById("posta");
		    	hs.onclick=function(){
				document.getElementById("sottoPosta").className=(document.getElementById("sottoPosta").className=="show") ? "hide" : "show";       	
			}			
		}
		</script>

            </div>
            </div>';//nav e menudx
            
    }

    function generaFooter() {
        echo '
            <div id="footer">
			<div id="validator">
			 <p>
				<a href="http://validator.w3.org/check?uri=referer"><img
					src="http://www.w3.org/Icons/valid-html401-blue"
					alt="Valid HTML 4.01 Transitional"></a>

			<a href="http://jigsaw.w3.org/css-validator/check/referer">
				<img style="border:0;width:88px;height:31px"
					src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
					alt="Valid CSS!">
			</a>
			</p>
			</div>







            </div>
        ';
    }

	function generaCategorie(){
		$qry= "SELECT Nome FROM Categorie ORDER BY Ordine";
		$check = mysql_query($qry);
		$array = mysql_fetch_assoc($check);
		$i=0;
		$l=mysql_num_rows($check);
		$args ="0";
		
		echo'<div id="cat">';
		while($array){
			$args.=",'".$array['Nome']."'";
			echo '<div id="c'.$i.'">';
		echo '<h3 class="categoria" id="'.$array['Nome'].'" ><a class="acat" name="'.$array['Nome'].'" id="c'.$i.'a" href="categoria.php?cat='.$array['Nome'].'">
		'.$array['Nome'].'</a></h3></div>';
		$i++;
		$array = mysql_fetch_assoc($check);



		}
		echo '</div>';
		if($_SESSION['Status']=='admin') echo "<script type=\"text/javascript\" > generaCategorie($args); </script>";
	}
	

	
	
	function generaThreads($threads){//si suppone i dati siano già stati assicurati
		$thread = mysql_fetch_assoc($threads);
		echo '
			<table class="threads">
				<tr class="titoliThread">
					<td>Data</td>
					<td>Titolo</td>
					<td>Autore</td>';
		if($_SESSION['Status']=='admin') {
		echo '
					<td>Modifica</td>
					<td>Cancella</td>';
		}
		echo '
				</tr>';
			
		while($thread){
			$data = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.substr($thread['Data'], 0, 5).'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>'.substr($thread['Data'], 5);
			echo '
			<tr>
			<td class="dataThread">'.$data.'</td>
			<td class="titoloThread"><a class="acat" href="thread.php?t='.$thread['Id'].'">'.$thread['Titolo'].'</a></td>
			<td class="autoreThread"><a class="utente" href="personal.php?u='.$thread['Autore'].'">'.$thread['Autore'].'</a></td>';

			if($_SESSION['Status']=='admin') { 	//Opzioni di modifica e cancellazione thread
			echo '
			<td><a href="actThread.php?cat='.$_GET['cat'].'&amp;t='.$thread['Id'].'&amp;action=modifica"><img src="images/modifica.gif" title="modifica thread" class="immThr" alt="modifica thread"></a></td>
			<td><a href="actThread.php?cat='.$_GET['cat'].'&amp;t='.$thread['Id'].'&amp;action=cancella" class="red" onclick="if(confirm(\'Rimuovere il thread?\')) 
											return true;
											return false;">X</a></td>';
			}
			echo '
			</tr>';
			$thread=mysql_fetch_assoc($threads);
		 }		
		 echo '</table>';
	}

	function generaPosts($posts){
		$post = mysql_fetch_assoc($posts);			
		echo '<table class="posts">';
		while($post){
			$qry = "SELECT Utente, Avatar FROM Utenti WHERE Utente = '".$post['Autore']."';";
			$utente = mysql_query($qry);
			$utente = mysql_fetch_assoc($utente);
			$corpo =  wordwrap(nl2br($post['Corpo']), 30, "\n", true);
			
			echo '
				<tr>
					<td class="infoPost"><div class="postAvatar"><img src="avatar/'.$utente['Avatar'].'" class="avatar" alt="avatar utente"></div><p>Autore: <a class="utente" href="personal.php?u='.$utente['Utente'].'">'.$utente['Utente'].'</a></p><p class="dataThread">Data: '.$post['Data'].'</p></td>
					<td class="postCorpo"><div>'.$corpo.'</div>';



			if(($post['Autore']==$_SESSION['Utente'] && (strcmp(date("Y-m-d H:i:s", mktime(date("H")-5, (date("i")-10), date("s"), date("m"), date("d"), date("Y"))), $post['Data']) < 0)) || $_SESSION['Status']=='admin') { 
				echo '
					<div class="modificaPost">';
				if($post['Autore']==$_SESSION['Utente'] && (strcmp(date("Y-m-d H:i:s", mktime(date("H")-5, (date("i")-10), date("s"), date("m"), date("d"), date("Y"))), $post['Data']) < 0))
					echo '<a href="modificaPost.php?p='.$post['Id'].'&amp;action=modifica"><img src="images/modifica.gif" title="modifica post" class="immThr" alt="modifica post"></a>';
				echo '
					<a href="modificaPost.php?p='.$post['Id'].'&amp;action=cancella" class="red" onclick="if(confirm(\'Rimuovere il post?\')) 
											return true;
											return false;">X</a>
					</div>';
			}
			echo '
					</td>
				</tr>';
			$post = mysql_fetch_assoc($posts);
		 }		
		 echo '</table>';
	}


	function generaHead(){
		echo '    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>News forum</title>

        <link rel="stylesheet" type="text/css" href="style.php">
        
        <script type="text/javascript">
       		 var x = screen.width;
			 var y = screen.height;        
			 document.write(\'<link rel="stylesheet" type="text/css" href="style.php?x=\'+x+\'&y=\'+y+\'">\');
        </script>

        
        <script type="text/javascript" src="function.js"></script>
		</head>';
		
		
	}







?>
